package codegenerator.generator;

import java.util.List;

import org.slf4j.Logger;

import baytony.util.StringUtil;
import baytony.util.Util;
import baytony.util.log.Log;
import codegenerator.CodeGeneratorException;
import codegenerator.ColumnInfo;
import codegenerator.CommonHelper;
import codegenerator.FileBean;
import codegenerator.JavaFileBean;
import codegenerator.TableInfo;

public class ControllerGenerator extends JavaGenerator {

private static final Logger logger = Log.getLogger();
	
	private static final String CONTROLLER_JTMP = "controller.jtmp";
	private static final String PROJECT_NAME = "#projectName#";
	private static final String CLASS_NAME = "#className#";
	private static final String DAO_NAME = "#daoName#";
	private static final String PARAM_ID_INFO = "#paramIdInfo#";
	private static final String CONTROLLER = "Controller";
	private static final String CLASS_PK_SET = "#classPkSet#";
	private static final String PARAM_ANNOTATION = "@RequestParam(value=\"#columnName#\") #type# #columnName#";
	private static final String CLASS_PK_INFO = "#classPkInfo#";
	
	
	
	
	@SuppressWarnings("unchecked")
	@Override
	public FileBean createInfo(TableInfo table) throws CodeGeneratorException {		
		String jtmpContent = super.readJtmp(CONTROLLER_JTMP);
		String tableName = table.getName();
		String className = CommonHelper.table2ClassName(tableName);
		String projectName = generatorHelper.getProjectName();
		logger.debug("create *controller.java : " + generatorHelper.getSrcPath() + "controller/" + className + CONTROLLER + ".java");
		jtmpContent = jtmpContent.replaceAll(PROJECT_NAME, projectName);
		jtmpContent = jtmpContent.replaceAll(CLASS_NAME, className);
		jtmpContent = jtmpContent.replaceAll(DAO_NAME, this.tarnsClassName2DaoName(className));
		jtmpContent = jtmpContent.replaceAll(PARAM_ID_INFO, this.createParamterStr(table.getPrimaryKey()));
		jtmpContent = jtmpContent.replaceAll(CLASS_PK_INFO, this.createPkInfo(table.getPrimaryKey(), className));
		jtmpContent = jtmpContent.replaceAll(CLASS_PK_SET, this.createPkSet(table.getPrimaryKey()));
		FileBean result = new JavaFileBean();
		result.setContent(jtmpContent);
		result.setFileName(className + CONTROLLER);
		result.setPath(generatorHelper.getSrcPath() + "controller/");
		return result;
	}
	
	/**
	 * 產生 pk set value 字串
	 *
	 * @param columns
	 * @return
	 */
	private String createPkSet(List<ColumnInfo> columns) {
		StringBuffer sb = new StringBuffer();
		if(columns.size() > 1) {
			for(ColumnInfo column : columns) {
				sb.append("pk.set");
				sb.append(CommonHelper.column2MethodName(column.getName()));
				sb.append(IGenerator.LEFT_SMALL_BRACKET);
				sb.append(CommonHelper.column2Attribute(column.getName()));
				sb.append(IGenerator.RIGHT_SMALL_BRACKET);
				sb.append(IGenerator.SEMICOLON);
				sb.append("\n\t\t");
			}
		}
		return sb.toString();
	}
	
	
	/**
	 * 產生 pk 的宣告
	 *
	 * @param columns
	 * @param className
	 * @return
	 * @throws NoPrimaryKeyException
	 */
	private String createPkInfo(List<ColumnInfo> columns, String className) throws NoPrimaryKeyException {
		if(Util.isEmptyList(columns))
			throw new NoPrimaryKeyException();
		StringBuffer sb = new StringBuffer();
		if(columns.size() == 1) {
			ColumnInfo column = columns.get(0);
			String pkType = CommonHelper.dbType2JavaType(column.getType());
			String columnName = CommonHelper.column2Attribute(column.getName());
			sb.append(pkType);
			sb.append(" pk = ");
			sb.append(columnName);
			sb.append(IGenerator.SEMICOLON);
			sb.append("\n");
		}
		else {
			String pkType = className + "Pk";
			sb.append(pkType);
			sb.append(" pk = new ");
			sb.append(pkType);
			sb.append(IGenerator.LEFT_SMALL_BRACKET);
			sb.append(IGenerator.RIGHT_SMALL_BRACKET);
			sb.append(IGenerator.SEMICOLON);
			sb.append("\n");
			/*for(ColumnInfo column : columns) {
				sb.append("pk.set");
				sb.append(CommonHelper.column2MethodName(column.getName()));
				sb.append(IGenerator.LIGHT_SMALL_BRACKET);
				sb.append(CommonHelper.column2Attribute(column.getName()));
				sb.append(IGenerator.RIGHT_SMALL_BRACKET);
				sb.append(IGenerator.SEMICOLON);
				sb.append("\n\t\t");
			}*/
		}
		return sb.toString();
	}	
	
	/**
	 * 產生parameter宣告
	 *
	 * @param columns
	 * @return
	 */
	private String createParamterStr(List<ColumnInfo>...columns) {
		StringBuffer sb = new StringBuffer();
		for(List<ColumnInfo> list : columns) {
			for(ColumnInfo column : list) {
				String paramStr = PARAM_ANNOTATION;
				paramStr = paramStr.replaceAll("#columnName#", CommonHelper.column2Attribute(column.getName()));
				paramStr = paramStr.replaceAll("#type#", CommonHelper.dbType2JavaType((column.getType())));
				sb.append(", ");
				sb.append(paramStr);	
			}
		}
		return sb.toString();
	}
	
	/**
	 * 把className轉換成DAO name
	 *
	 * @param className
	 * @return
	 * @throws CodeGeneratorException
	 */
	private String tarnsClassName2DaoName(String className) throws CodeGeneratorException {
		if(StringUtil.isEmptyString(className)) {
			throw new CodeGeneratorException(); 
		}
		String i = className.substring(0,1);
		return i.toLowerCase() + className.substring(1);
	}

}
